'use strict'

$(document).ready(function() {
    var queryString = location.search;
    var parameters = Request.parseQueryString(queryString);

    var queryDate = null;

    if ( parameters.date ) {
        queryDate = new Date(Number(parameters.date));
    }

    var $inputDate = $('.date_input_top').datepicker({
    });
    $inputDate.datepicker('update', new Date());
    if ( queryDate ) {
        $inputDate.datepicker('update', queryDate);
    }

    var $inputStatus = $('#teacher_search_status');
    var status = parameters.status;
    if ( 'status' in parameters ) {
        $inputStatus.val(status);
        var statusName = $('.menu-status a[value="' + status + '"]').text();
        $inputStatus.parent('a').find('span').text(statusName);
    }

    var $inputStudentId = $('.input-sid');
    if ( parameters.sid ) {
        $inputStudentId.val(parameters.sid);
    }

    var $inputStudentName = $('.input-name');
    if ( parameters.name ) {
        $inputStudentName.val(decodeURI(parameters.name));
    }

    var $inputNatureClass = $('#teacher_search_class');
    var natureClass = parameters.nature_class;
    if ( natureClass ) {
        $inputNatureClass.val(natureClass);
        var natureClassName = $('.menu-class a[value="' + natureClass + '"]').text();
        $inputNatureClass.parent('a').find('span').text(natureClassName);
    }

    $('.btn-search').click(function(e) {
        disableAll(e);

        var date = $inputDate.datepicker('getDate');
        var status = $inputStatus.val();
        var sid = $inputStudentId.val();
        var name = $inputStudentName.val();
        var natureClass = $inputNatureClass.val();

        var queryCondition = {
            date: date.getTime(),
            status: status
        };

        if ( sid ) {
            queryCondition.sid = sid;
        }

        if ( name ) {
            queryCondition.name = name;
        }

        if ( natureClass != '0' ) {
            queryCondition.nature_class = natureClass;
        }

        var request = new Request('/teacher/manual/manual', queryCondition);
        var response = new Response();
        response.redirectTo(request);
    });
});

var tableEditData = [];

function editButtonClicked(event) {
    var hiddenSelect = $(".table_select");
    for (var i=0; i<hiddenSelect.length; i++) {
        var select = hiddenSelect[i];
        if (select.getAttribute('data-status') != '--') {
            $(select).removeClass('table_select_hide');
        }
    }
    $("#edit_before").hide();
    $("#edit_after").show();

    $(".teacher_search_table_div").removeClass('teacher_search_td_default');
}

function cancelButtonClicked(event) {

    $("div.recording_table_div li").removeClass('active');
    for (var i=0; i<tableEditData.length; i++) {
        var originalStatus = findOriginalStatus(tableEditData[i]);
        $("[data-id="+"status_text_enter"+originalStatus.id+"]")[0].innerHTML = getStatusText(originalStatus.enterStatus);
        $("[data-id="+"status_text_exit"+originalStatus.id+"]")[0].innerHTML = getStatusText(originalStatus.exitStatus);
    }

    var hiddenSelect = $(".table_select");
    hiddenSelect.addClass('table_select_hide');
    $("#edit_before").show();
    $("#edit_after").hide();

    $(".teacher_search_table_div").addClass('teacher_search_td_default');
}

function saveButtonClicked(event) {

    for (var i=0; i<tableEditData.length; i++) {
        if (!tableEditData[i].enterStatus || !tableEditData[i].exitStatus) {
            var originalStatus = findOriginalStatus(tableEditData[i]);
            if (!tableEditData[i].enterStatus) {
                tableEditData[i].enterStatus = originalStatus.enterStatus;
            }
            if (!tableEditData[i].exitStatus) {
                tableEditData[i].exitStatus = originalStatus.exitStatus;
            }
        }
    }

    var request = new Request('/teacher/attendance/update');
    request.postJSON({
        values: tableEditData
    }, function(result) {
        if ( !result.successful ) {
            alert('保存失败');
            return;
        }

        var hiddenSelect = $(".table_select");
        hiddenSelect.addClass('table_select_hide');
        $("#edit_before").show();
        $("#edit_after").hide();
    });

    $(".teacher_search_table_div").addClass('teacher_search_td_default');
}

function editStatusClicked(event) {
    var target = event.currentTarget;
    var status = {
        id: target.parentNode.parentNode.parentNode.parentNode.getAttribute('data-id')
    };
    if (target.parentNode.parentNode.parentNode.parentNode.getAttribute('data-type') == 'exit') {
        status.exitStatus = target.getAttribute('value');
    }
    else {
        status.enterStatus = target.getAttribute('value');
    }
    var position = findStatusInData(status);
    if (position < 0) {
        tableEditData.push(status);
    }
    else {
        if (status.enterStatus) {
            tableEditData[position].enterStatus = status.enterStatus;
        }
        if (status.exitStatus) {
            tableEditData[position].exitStatus = status.exitStatus;
        }
    }
}

function findStatusInData(status) {
    for (var i=0; i<tableEditData.length; i++) {
        if (status.id == tableEditData[i].id) {
            return i;
        }
    }
    return -1;
}

function findOriginalStatus(status) {
    for (var i=0; i<Attendances.length; i++) {
        for (var j=0; j<Attendances[i].values.length; j++) {
            if (Attendances[i].values[j].id == status.id) {
                return Attendances[i].values[j];
            }
        }
    }
}

function getStatusText(status) {
    if (status == 1) {
        return '出勤';
    }
    else if (status == -2) {
        return '迟到';
    }
    else if (status == -3) {
        return '早退';
    }
    else if (status == 3) {
        return '请假';
    }
    else if (status == -1) {
        return '旷课';
    }
}
